package com.eshopkeeper.web.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.eshopkeeper.core.base.BaseSysController;
import com.eshopkeeper.core.util.RedisUtil;
import com.eshopkeeper.core.util.Request2ModelUtil;
import com.eshopkeeper.core.util.WebUtil;
import com.eshopkeeper.model.sys.SysMenu;
import com.eshopkeeper.model.sys.ext.SysMenuBean;
import com.eshopkeeper.service.sys.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author chenshufeng
 * @Description: 菜单管理
 * @date 2017年3月7日
 */
@RestController
@Api(value = "菜单管理", description = "菜单管理")
@RequestMapping(value = "menu", method = RequestMethod.POST)
public class SysMenuController extends BaseSysController {
    @Autowired
    private SysMenuService sysMenuService;

    // 查询菜单
    @ApiOperation(value = "查询菜单")
    //@RequiresPermissions("sys.menu.read")
    @RequestMapping(value = "/read/list")
    public Object get(HttpServletRequest request, ModelMap modelMap) {
        Map<String, Object> params = WebUtil.getParameterMap(request);
        Page<?> list = sysMenuService.queryMenuList(params);
        return setSuccessModelMap(modelMap, list);
    }

    // 详细信息
    @ApiOperation(value = "菜单详情")
    //@RequiresPermissions("sys.menu.read")
    @RequestMapping(value = "/read/detail")
    public Object detail(ModelMap modelMap, @RequestParam(value = "id", required = false) Long id) {
        SysMenu record = sysMenuService.queryById(id);
        return setSuccessModelMap(modelMap, record);
    }

    // 新增菜单
    @ApiOperation(value = "添加菜单")
    //@RequiresPermissions("sys.menu.add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object add(HttpServletRequest request, ModelMap modelMap) {
        SysMenu record = Request2ModelUtil.covert(SysMenu.class, request);
        sysMenuService.add(record);
        return setSuccessModelMap(modelMap);
    }

    // 修改菜单
    @ApiOperation(value = "修改菜单")
    //@RequiresPermissions("sys.menu.update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(HttpServletRequest request, ModelMap modelMap) {
        SysMenu record = Request2ModelUtil.covert(SysMenu.class, request);
        sysMenuService.update(record);
        return setSuccessModelMap(modelMap);
    }

    //批量更新排序号
    @ApiOperation(value = "批量更新排序号")
    //@RequiresPermissions("sys.menu.editSort")
    @RequestMapping(value = "/editSort", method = RequestMethod.POST)
    public Object editSort(ModelMap modelMap, @RequestParam(value = "ids", required = false) String ids) {
        String[] idsArray = ids.split(",");
        Long userId = getCurrUser();
        for (int i = 0; i < idsArray.length; i++) {
            String idAndSort = idsArray[i];
            SysMenuBean sysMenuBean = new SysMenuBean();
            sysMenuBean.setId(Long.valueOf(idAndSort.split("-")[0]));
            sysMenuBean.setSortNo(Long.valueOf(idAndSort.split("-")[1]));
            sysMenuBean.setUpdateBy(userId);
            sysMenuService.updateMenuById(sysMenuBean);
        }
        RedisUtil.del("menuList");
        return setSuccessModelMap(modelMap);
    }

    // 删除菜单
    @ApiOperation(value = "删除菜单")
    //@RequiresPermissions("sys.menu.delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object delete(HttpServletRequest request, ModelMap modelMap,
                         @RequestParam(value = "id", required = false) Long id) {
        sysMenuService.delete(id);
        return setSuccessModelMap(modelMap);
    }

    // 获取所有权限
    @ApiOperation(value = "获取所有权限")
    //@RequiresPermissions("sys.menu.read")
    @RequestMapping(value = "/read/permission")
    public Object getPermissions(HttpServletRequest request, ModelMap modelMap) {
        List<Map<String, String>> permissions = sysMenuService.getPermissions();
        return setSuccessModelMap(modelMap, permissions);
    }
    
    // 查询父级菜单
    @RequestMapping(value = "/selParentNameList", method = RequestMethod.POST)
    public Object selParentNameList(HttpServletRequest request, ModelMap modelMap,
                                    @RequestParam(value = "id", required = false) Long id) {
        List<Map<String, Object>> parentList = sysMenuService.queryParentList();
        return setSuccessModelMap(modelMap,parentList);
    }
    
}
